package com.mall.controller;

import com.mall.common.PageResult;
import com.mall.common.Result;
import com.mall.dto.ProductQueryDTO;
import com.mall.service.ProductService;
import com.mall.vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品控制器
 * 
 * @author Mall Team
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 分页查询商品列表
     */
    @GetMapping("/list")
    public Result<PageResult<ProductVO.ProductListVO>> getProductList(@Valid ProductQueryDTO queryDTO) {
        PageResult<ProductVO.ProductListVO> pageResult = productService.getProductPage(queryDTO);
        return Result.success(pageResult);
    }
    
    /**
     * 获取商品详情
     */
    @GetMapping("/{id}")
    public Result<ProductVO.ProductDetailVO> getProductDetail(@PathVariable Long id) {
        ProductVO.ProductDetailVO productDetail = productService.getProductDetail(id);
        return Result.success(productDetail);
    }
    
    /**
     * 搜索商品
     */
    @GetMapping("/search")
    public Result<PageResult<ProductVO.ProductListVO>> searchProducts(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<ProductVO.ProductListVO> pageResult = 
            productService.searchProducts(keyword, pageNum, pageSize);
        return Result.success(pageResult);
    }
    
    /**
     * 根据分类获取商品
     */
    @GetMapping("/category/{categoryId}")
    public Result<PageResult<ProductVO.ProductListVO>> getProductsByCategory(
            @PathVariable Long categoryId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<ProductVO.ProductListVO> pageResult = 
            productService.getProductsByCategory(categoryId, pageNum, pageSize);
        return Result.success(pageResult);
    }
    
    /**
     * 获取推荐商品
     */
    @GetMapping("/recommend")
    public Result<List<ProductVO.ProductListVO>> getRecommendProducts(
            @RequestParam(defaultValue = "10") Integer limit) {
        List<ProductVO.ProductListVO> products = productService.getRecommendProducts(limit);
        return Result.success(products);
    }
    
    /**
     * 获取热销商品
     */
    @GetMapping("/hot")
    public Result<List<ProductVO.ProductListVO>> getHotProducts(
            @RequestParam(defaultValue = "10") Integer limit) {
        List<ProductVO.ProductListVO> products = productService.getHotProducts(limit);
        return Result.success(products);
    }
    
    /**
     * 获取相关商品
     */
    @GetMapping("/{id}/related")
    public Result<List<ProductVO.ProductListVO>> getRelatedProducts(
            @PathVariable Long id,
            @RequestParam(defaultValue = "6") Integer limit) {
        // 需要先获取商品信息得到分类ID
        ProductVO.ProductDetailVO product = productService.getProductDetail(id);
        List<ProductVO.ProductListVO> relatedProducts = 
            productService.getRelatedProducts(product.getCategoryId(), id, limit);
        return Result.success(relatedProducts);
    }
}